﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
    public int id;

    Rigidbody2D rb;

    public GameObject Parent_element;
    public SpriteRenderer element_1;
    public SpriteRenderer element_2;
    public SpriteRenderer element_3;
    public SpriteRenderer element_4;
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    public void Refresh()
    {
        Parent_element.gameObject.SetActive(id > 0);

        element_1.gameObject.SetActive(id == 1);
        element_2.gameObject.SetActive(id == 2);
        element_3.gameObject.SetActive(id == 3);
        element_4.gameObject.SetActive(id == 4);
    }

    void Update()
    {
        if (rb.velocity.y < 0)
        {
            rb.gravityScale = 0;
            rb.velocity = Vector2.zero;

            //bool isHit = false;
            //RaycastHit2D[] hits = CellFunctions.RaycastAll(transform.position, Vector2.down, 0f, Color.yellow);
            //Debug.Log("碰到的物体: " + hits.Length);
            //foreach (var hit in hits)
            //{
            //    if (hit.collider.gameObject != this.gameObject)
            //    {
            //        Debug.Log(hit.collider.gameObject);
            //        isHit = true;
            //        Destroy(this.gameObject);
            //        Cell cell = hit.collider.gameObject.GetComponent<Cell>();
            //        cell.id = this.id;
            //        cell.Refresh();

            //        GameManager.Instance.currentBullet = CellFunctions.CreateBullet();

            //        //CellFunctions.Check();

            //        break;
            //    }
            //}

            //if (isHit == false)
            //{
            //    Destroy(this.gameObject);
            //    GameManager.Instance.currentBullet = CellFunctions.CreateBullet();
            //}
        }
    }
}
